-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

]]--
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local assets =
{
    Asset("ANIM", "anim/loramia_building_analytic_creation.zip"),
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 天赋检查
    local function is_player_talent_unlocked(doer)
        if doer and doer.replica.loramia_com_talent_tree and doer.replica.loramia_com_talent_tree:IsUnlocked("evolution_analysis") then
            return true
        end
        return false
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 
    local function book_research_station_fn(player)
        player.components.builder:GiveTempTechBonus({SCIENCE = 2, MAGIC = 2, SEAFARING = 2})

        local fx = SpawnPrefab(player.components.rider ~= nil and player.components.rider:IsRiding() and "fx_book_research_station_mount" or "fx_book_research_station")
        fx.Transform:SetPosition(player.Transform:GetWorldPosition())
        fx.Transform:SetRotation(player.Transform:GetRotation())
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- workable
    local function isDigging(doer)
        local tag = ACTIONS.DIG.id.."_tool"
        return doer and doer.replica.inventory and doer.replica.inventory:EquipHasTag(tag)
    end

    local function special_workable_install(inst)
        inst:ListenForEvent("Loramia_OnEntityReplicated.loramia_com_workable",function(inst,replica_com)
            replica_com:SetTestFn(function(inst,doer,right_click)
                if right_click and is_player_talent_unlocked(doer) then
                    replica_com:SetSGAction("dolongaction")
                    replica_com:SetText("loramia_building_analytic_creation",STRINGS.ACTIONS.WRAPBUNDLE)
                    return true
                end
                if right_click and isDigging(doer) then
                    replica_com:SetSGAction("dig_start")
                    replica_com:SetText("loramia_building_analytic_creation",STRINGS.ACTIONS.DIG)
                    return true
                end
                return false
            end)
            replica_com:SetSGAction("dig_start")
            replica_com:SetText("loramia_building_analytic_creation",STRINGS.ACTIONS.DIG)
        end)
        if not TheWorld.ismastersim then
            return
        end

        inst:AddComponent("loramia_com_workable")
        inst.components.loramia_com_workable:SetOnWorkFn(function(inst,doer)
            -----------------------------------------------------------
            --- 打包
                if is_player_talent_unlocked(doer) then
                    local item = SpawnPrefab("loramia_building_analytic_creation_kit")
                    local record = inst:GetSaveRecord()
                    item.Transform:SetPosition(inst.Transform:GetWorldPosition())
                    item.components.loramia_data:Set("record",record)
                    inst:Remove()
                    return true
                end
            -----------------------------------------------------------
            --- 挖掘
                if isDigging(doer) then
                    inst:AddComponent("lootdropper")
                    for i = 1, 3, 1 do
                        inst.components.lootdropper:SpawnLootPrefab("goldnugget")
                        inst.components.lootdropper:SpawnLootPrefab("transistor")
                        inst.components.lootdropper:SpawnLootPrefab("cutstone")
                    end
                    local fx = SpawnPrefab("collapse_small")
                    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
                    inst:Remove()
                    return true
                end
            -----------------------------------------------------------
        end)

    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 积雪
    local function snow_init(inst)
        if TheWorld.state.issnowcovered then
            -- inst.AnimState:Show("SNOW")
            inst.AnimState:ShowSymbol("snow")
        else
            -- inst.AnimState:Hide("SNOW")
            inst.AnimState:HideSymbol("snow")
        end    
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- workable
    local function OnFinishCallback(inst,worker)


    end
    local function onhit(inst,worker)        
        inst.AnimState:PlayAnimation("hit",false)
        inst.AnimState:PushAnimation("idle",true)
        inst.components.workable:SetWorkLeft(1)
        if not (worker and worker:HasTag("player") ) then
            return
        end
        if inst.components.loramia_com_power_battery:IsFull() then
            inst.components.loramia_com_power_battery:DoDelta(-100)
            book_research_station_fn(worker)
            return
        else
            inst.components.loramia_com_power_battery:DoDelta(TUNING.LORAMIA_DEBUGGING_MODE and 20 or 5)    
        end        
    end
    local function workable_install(inst)
        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(1)
        inst.components.workable:SetOnWorkCallback(onhit)
        inst.components.workable:SetOnFinishCallback(OnFinishCallback)
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- fx 特效
    local function fx_create(inst)
        local fx = SpawnPrefab("loramia_building_analytic_creation_fx")
        fx.entity:SetParent(inst.entity)
        fx.entity:AddFollower()
        fx.Follower:FollowSymbol(inst.GUID, "rock_ball",0,0,0,true)
        return fx
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 电池安装
    local function battery_com_install(inst)
        inst:ListenForEvent("Loramia_OnEntityReplicated.loramia_com_power_battery",function(inst,replica_com)
            replica_com:SetTextBoxCustomUpdateFn(function(inst,root)
                if not ( root and root.txt_box ) then
                    return
                end
                local txt_box = root.txt_box
                if ThePlayer and inst:GetDistanceSqToInst(ThePlayer) < (inst:HasTag("evoluted") and 100 or 6.5) then
                    local s_pt_x,s_pt_y= TheSim:GetScreenPos(inst.Transform:GetWorldPosition()) -- 左下角为原点。
                    root:SetPosition(s_pt_x,s_pt_y-40,0)
                    root:Show()
                    local current = inst.replica.loramia_com_power_battery:GetCurrent() or 0
                    local max = inst.replica.loramia_com_power_battery:GetMax() or 100
                    txt_box:SetString(string.format("%3d", current).."/"..max)
                    txt_box:Show()
                else
                    txt_box:Hide()
                    return
                end
            end)
            replica_com:SetTextColour(101/255,171/255,216/255)
        end)
        if not TheWorld.ismastersim then
            return
        end
        inst:AddComponent("loramia_com_power_battery")
        inst.components.loramia_com_power_battery:SetMax(100)
        inst.components.loramia_com_power_battery:Set_Ignore_All_Devices(true)
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- building
    local function OnBuilt(inst,player)
        if is_player_talent_unlocked(player) then
            inst.components.loramia_data:Set("evoluted",true)
        else
            inst.components.loramia_data:Set("evoluted",false)
        end
    end
    local function evoluted_task_fn(inst)
        if not inst.components.loramia_data:Get("evoluted") then
            return
        end
        inst.components.loramia_com_power_battery:DoDelta(5)
    end
    local function on_load_tag_init(inst)
        if inst.components.loramia_data:Get("evoluted") then
            inst:AddTag("evoluted")
        else
            inst:RemoveTag("evoluted")
        end
    end
    local function fn()
        local inst = CreateEntity()
        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddLight()
        inst.entity:AddSoundEmitter()
        inst.entity:AddMiniMapEntity()
        inst.entity:AddNetwork()
        MakeObstaclePhysics(inst, 1)
        inst.entity:AddDynamicShadow()
        inst.DynamicShadow:SetSize(2, 1)
        inst.MiniMapEntity:SetIcon("loramia_building_analytic_creation.tex")
        inst.AnimState:SetBank("loramia_building_analytic_creation")
        inst.AnimState:SetBuild("loramia_building_analytic_creation")
        inst.AnimState:PlayAnimation("idle")
        inst.AnimState:HideSymbol("shadow")
        inst.AnimState:OverrideSymbol("rock_ball","loramia_building_analytic_creation","empty")
        inst.entity:SetPristine()
        -----------------------------------------------------------
        --- 
            special_workable_install(inst)
            battery_com_install(inst)
        -----------------------------------------------------------
        ---
            if not TheWorld.ismastersim then
                return inst
            end
        -----------------------------------------------------------
        --- 
            inst:AddComponent("loramia_data")
        -----------------------------------------------------------
        --- 
            inst:AddComponent("inspectable")
        -----------------------------------------------------------
        --- 
            inst:DoPeriodicTask(1,evoluted_task_fn)
        -----------------------------------------------------------
        --- 官方的workable
            workable_install(inst)
        -----------------------------------------------------------
        --- 
            inst:DoTaskInTime(0,fx_create)
        -----------------------------------------------------------
        --- 积雪
            inst:WatchWorldState("issnowcovered", snow_init)
            snow_init(inst)
        -----------------------------------------------------------
        --- 
            inst.OnBuilt = OnBuilt
        -----------------------------------------------------------
        ---
            inst:DoTaskInTime(0,on_load_tag_init)
        -----------------------------------------------------------

        MakeHauntableLaunch(inst)

        return inst
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- fx
    local function fx()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        inst.AnimState:SetBank("loramia_building_analytic_creation")
        inst.AnimState:SetBuild("loramia_building_analytic_creation")
        inst.AnimState:PlayAnimation("rock_ball",true)

        inst:AddTag("FX")
        inst:AddTag("fx")
        inst:AddTag("INLIMBO")

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        return inst
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- item
    local function item_fn()
        local inst = CreateEntity()
        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddMiniMapEntity()
        inst.entity:AddNetwork()
        MakeInventoryPhysics(inst)
        MakeInventoryFloatable(inst, "med", 0.05, {0.85, 0.45, 0.85})
        inst.AnimState:SetBank("loramia_building_analytic_creation")
        inst.AnimState:SetBuild("loramia_building_analytic_creation")
        inst.AnimState:PlayAnimation("item")
        inst:AddTag("deploykititem")
        inst.entity:SetPristine()
        if not TheWorld.ismastersim then
            return inst
        end
        --------------------------------------------------------------------------
        ---
            inst:AddComponent("loramia_data")
        --------------------------------------------------------------------------
        ------ 物品名 和检查文本
            inst:AddComponent("inspectable")
            inst:AddComponent("inventoryitem")
            -- inst.components.inventoryitem:ChangeImageName("leafymeatburger")
            inst.components.inventoryitem.imagename = "loramia_building_analytic_creation_kit"
            inst.components.inventoryitem.atlasname = "images/inventoryimages/loramia_building_analytic_creation_kit.xml"
        -------------------------------------------------------------------
            MakeHauntableLaunch(inst)
        -------------------------------------------------------------------
        --- 落水影子
            local function shadow_init(inst)
                if inst:IsOnOcean(false) then       --- 如果在海里（不包括船）
                    -- inst.AnimState:Hide("SHADOW")
                    inst.AnimState:HideSymbol("shadow")
                else                                
                    -- inst.AnimState:Show("SHADOW")
                    inst.AnimState:ShowSymbol("shadow")
                end
            end
            inst:ListenForEvent("on_landed",shadow_init)
            shadow_init(inst)
        -------------------------------------------------------------------
        --- 积雪
            inst:WatchWorldState("issnowcovered", snow_init)
            snow_init(inst)
        -------------------------------------------------------------------
        --- 放置
            inst:AddComponent("deployable")
            inst.components.deployable.ondeploy = function(inst,pt,player)
                local record = inst.components.loramia_data:Get("record")
                local ret_inst = nil
                if record == nil then
                    ret_inst = SpawnPrefab("loramia_building_analytic_creation")
                else
                    ret_inst = SpawnSaveRecord(record)
                end
                ret_inst.Transform:SetPosition(pt.x,0,pt.z)
                ret_inst:OnBuilt(player)
                inst:Remove()
            end
            inst.components.deployable:SetDeploySpacing(DEPLOYSPACING.NONE)
            inst.components.deployable.restrictedtag = "loramia"
        -------------------------------------------------------------------
        --- 特效
            inst:DoTaskInTime(0,fx_create)
        -------------------------------------------------------------------
        return inst
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- placer
    local function placer_postinit_fn(inst)
        snow_init(inst)
        local fx = fx_create(inst)
        inst.AnimState:OverrideSymbol("rock_ball","loramia_building_analytic_creation","empty")
        inst.components.placer:LinkEntity(fx)
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


return Prefab("loramia_building_analytic_creation", fn, assets),
    Prefab("loramia_building_analytic_creation_fx", fx, assets),
    Prefab("loramia_building_analytic_creation_kit", item_fn, assets),
    MakePlacer("loramia_building_analytic_creation_kit_placer", "loramia_building_analytic_creation", "loramia_building_analytic_creation", "idle", nil, nil, nil, nil, nil, nil, placer_postinit_fn)


